﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TexasHoldem.RankedHands
{
    public class FullHouseHand: RankedHand
    {
        private FullHouseHand(Hand hand) : base(hand) { }
        public static FullHouseHand TryCreateFrom(Hand hand)
        {
            return FindFullHouse(hand.Cards) != null ? new FullHouseHand(hand) : null;
        }

        public override string RankName
        {
            get { return "Full House"; }
        }

        protected override ushort RankingStrength
        {
            get { return 6; }
        }

        protected override IEnumerable<Card> FindDefiningCards()
        {
            return FindFullHouse(Cards);
        }

        static IEnumerable<Card> FindFullHouse(IEnumerable<Card> cards)
        {
            var groups = cards.GroupBy(c => c.Value);
            if (!groups.Any(g => g.Count() >= 3)) return null;
            var trips = groups.Where(g => g.Count() >= 3).OrderByDescending(g => g.Key).First();

            var remainingGroups = groups.Where(g => g.Key != trips.Key);
            if (!remainingGroups.Any(g => g.Count() >= 2)) return null;
            var pair = remainingGroups.Where(g => g.Count() >= 2).OrderByDescending(g => g.Key).First();
            
            return trips.Union(pair);
        }
    }
}
